package com.astrofizzbizz.stoneedgegui.app;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import com.astrofizzbizz.simpleXml.SimpleXmlException;
import com.astrofizzbizz.simpleXml.SimpleXmlReader;
import com.astrofizzbizz.stoneedgegui.utilities.StoneEdgeIIIException;




public class AstroCatalog 
{
	private ArrayList<AstroTarget> targetList = new ArrayList<AstroTarget>();
	private StoneEdgeIIIGui stoneEdgeIIIGui;
	private JPanel catalogPanel;
	private int selectedTarget = -1;
	
	public AstroCatalog( String xmlFileName, StoneEdgeIIIGui stoneEdgeIIIGui) throws StoneEdgeIIIException 
	{
		selectedTarget = -1;
		this.stoneEdgeIIIGui = stoneEdgeIIIGui;
		
		try 
		{
			SimpleXmlReader xmlReader = new SimpleXmlReader(xmlFileName);
	//		getDate(xmlReader);
			int numTargets = xmlReader.tagsByName("target").numChildTags();
			SimpleXmlReader readerList = xmlReader.tagsByName("target");
			for (int ii = 0; ii < numTargets; ++ii)
			{
				SimpleXmlReader targetTag = readerList.tag(ii);
				targetList.add(new AstroTarget(targetTag, stoneEdgeIIIGui, ii));
			}
			createScrollPane();
		}
		catch (SimpleXmlException e)
		{
			throw new StoneEdgeIIIException("SimpleXmlException", e);
		}
	}
	public AstroCatalog(InputStream inputstream, StoneEdgeIIIGui stoneEdgeIIIGui) throws StoneEdgeIIIException 
	{
		selectedTarget = -1;
		this.stoneEdgeIIIGui = stoneEdgeIIIGui;
		
		try 
		{
			SimpleXmlReader xmlReader = new SimpleXmlReader(inputstream);
	//		getDate(xmlReader);
			int numTargets = xmlReader.tagsByName("target").numChildTags();
			SimpleXmlReader readerList = xmlReader.tagsByName("target");
			for (int ii = 0; ii < numTargets; ++ii)
			{
				SimpleXmlReader targetTag = readerList.tag(ii);
				targetList.add(new AstroTarget(targetTag, stoneEdgeIIIGui, ii));
			}
			createScrollPane();
		}
		catch (SimpleXmlException e)
		{
			throw new StoneEdgeIIIException("SimpleXmlException", e);
		}
	}
	public void setSelectedTarget(int itarget) throws StoneEdgeIIIException
	{
//TODO when user puts in RA & Dec set target to -1 - what happens on nudge? - set rad and dec on target method
		if (selectedTarget > -1)
		{
			targetList.get(selectedTarget).getTargetButton().setSelected(false);
			if (itarget == selectedTarget)
			{
				selectedTarget = -1;
				return;
			}
		}
		if (itarget < 0)
		{
			selectedTarget = -1;
			return;
		}
		selectedTarget = itarget;
		targetList.get(selectedTarget).getTargetButton().setSelected(true);
		stoneEdgeIIIGui.getTargetingPanel().setAstroTarget(targetList.get(selectedTarget));

		stoneEdgeIIIGui.getStatusBar().setText(targetList.get(selectedTarget).getName() + " Selected");
		stoneEdgeIIIGui.getMainPane().setSelectedIndex(
				stoneEdgeIIIGui.getMainPane().indexOfTab(stoneEdgeIIIGui.getTargetTabTitle()));
	}
	public void setSize(int width, int height)
	{
		catalogPanel.setPreferredSize(new Dimension(width, height));
	}

	private void createScrollPane()
	{
		JPanel headerPanel = new JPanel();
		headerPanel.setLayout(new GridLayout(1,8));
		JLabel selectLabel  = new JLabel("Select");
		JLabel targetLabel  = new JLabel("Target");
		JLabel raLabel = new JLabel("RA");
		JLabel decLabel = new JLabel("Dec");
		JLabel altLabel = new JLabel("Alt");
		JLabel azLabel = new JLabel("AZ");
		JLabel airMassLabel = new JLabel("Air Mass");
		JLabel infoLabel = new JLabel("info");
		selectLabel.setHorizontalAlignment(SwingConstants.CENTER);
		targetLabel.setHorizontalAlignment(SwingConstants.CENTER);
		raLabel.setHorizontalAlignment(SwingConstants.CENTER);
		decLabel.setHorizontalAlignment(SwingConstants.CENTER);
		altLabel.setHorizontalAlignment(SwingConstants.CENTER);
		azLabel.setHorizontalAlignment(SwingConstants.CENTER);
		airMassLabel.setHorizontalAlignment(SwingConstants.CENTER);
		infoLabel.setHorizontalAlignment(SwingConstants.CENTER);
		selectLabel.setForeground(Color.BLUE);
		targetLabel.setForeground(Color.BLUE);
		raLabel.setForeground(Color.BLUE);
		decLabel.setForeground(Color.BLUE);
		altLabel.setForeground(Color.BLUE);
		azLabel.setForeground(Color.BLUE);
		airMassLabel.setForeground(Color.BLUE);
		infoLabel.setForeground(Color.BLUE);
		headerPanel.add(selectLabel);
		headerPanel.add(targetLabel);
		headerPanel.add(raLabel);
		headerPanel.add(decLabel);
		headerPanel.add(altLabel);
		headerPanel.add(azLabel);
		headerPanel.add(airMassLabel);
		headerPanel.add(infoLabel);
		
		JPanel targetsPanel = new JPanel();
		targetsPanel.setLayout(new BoxLayout(targetsPanel, BoxLayout.Y_AXIS));
		for (int ii = 0; ii < targetList.size(); ++ii)
		{
			targetsPanel.add(targetList.get(ii).getMenuPanel());
		}
		JScrollPane scrollPane = new JScrollPane(targetsPanel);
		
		catalogPanel = new JPanel();
		catalogPanel.setLayout(new BoxLayout(catalogPanel, BoxLayout.Y_AXIS));
		catalogPanel.add(headerPanel);
		catalogPanel.add(scrollPane);
	}
	public void upDate() throws StoneEdgeIIIException 
	{
		for (int ii = 0; ii < targetList.size(); ++ii)
		{
			targetList.get(ii).update();
		}

	}
	public JPanel getCatalogPanel() {return catalogPanel;}
	public StoneEdgeIIIGui getStoneEdgeIIIGui() {return stoneEdgeIIIGui;}
	public static void main(String[] args) throws StoneEdgeIIIException, IOException, URISyntaxException
	{
	}

}
